export type T_ID=string;
export class CacheData<T extends ICacheData>
{
    cacheData:T[]=[];
    constructor(Tdata:Array<T>)
    {
        this.cacheData = Tdata;
    }

}

export abstract class APollingTask<T>
{
    code!:T_ID;
    value?:T;
    lastupdate:number=0;

    // roomName!:string;

    constructor(roomName:string)
    {
        this.code = roomName;
    }
    // 是否过期了
    public IsStale():boolean
    {
        if(this.lastupdate<0)
        {
            return false;
        }
        return this.lastupdate<Game.time;
    }
    protected abstract Update(...args:any[]):void;
    public getValue(forceUpdate:boolean=false):T|undefined
    {
        if(this.IsStale() || forceUpdate)
        {
            this.Update(this.code);
        }
        return this.value;
    }
}


export   class APollingStation<T,U extends APollingTask<T>>
{
    // code就是房间号
    tasks:Record<T_ID,U>={};
    public RegisterTask(task:U)
    {
        this.tasks[task.code]=task;
    }

    public Poll(code:T_ID,forceUpdate:boolean=false):T|undefined|false
    {
        if(this.tasks[code])
        {
            return this.tasks[code].getValue(forceUpdate);
        }
        return false;
    }
}
